<?php

namespace C\L;

class WebController extends Controller
{
    protected function checkLogin()
    {
        $uri = explode('?', $_SERVER['REQUEST_URI'])[0];

        $allowUrls = $this->config->get('config')->public_api_urls->toArray();

        if (!in_array($uri, $allowUrls) && !$this->uid) {
            $this->error(null, 'E1001');
        }

        if ($this->uid <= 0) {
            return true;
        }

        $this->cache->setex('ssid_' . $this->uid, $this->ssidKey, 48 * 3600);

        $status = $this->s_user->getValue('status', $this->uid);
        switch ($status) {
            case 'N':
                return $this->error(null, 'E1005');
            case 'S':
                return $this->error(null, 'E1006');
            default:
                return true;
        }
    }

    protected function paginate($model, $currentPage, $perPage, $params)
    {
        $count = $model::count($params);

        $pagination = [
            'total'        => (int) $count,
            'per_page'     => (int) $perPage,
            'total_page'   => (int) ceil($count / $perPage),
            'current_page' => (int) $currentPage,
        ];

        return $pagination;
    }
}
